
#include "app_includes.h"

void osc_set_trigger(uint8_t type, int16_t value)
{
    int32_t duty;
    
    oscilloscope.config.trigger_value = value;
    duty = (value*4095/3300 + oscilloscope.meter.offset)*100/4095;
    if(duty<0)
        duty = 0;
    else if(duty>100)
        duty = 100;
    board_set_pwm_duty(kBoardPWM_OscOffset, duty);
    
    oscilloscope.config.trigger_type = type;
    board_osc_trigger_config(oscilloscope.config.trigger_type);
}

void osc_select_ch(uint8_t ch, uint8_t amp)
{
    oscilloscope.config.v_div = amp>7 ? 7:amp;
    oscilloscope.config.ch_select = ch;
    
    board_osc_config(oscilloscope.config.ch_select, oscilloscope.config.v_div);
}

void osc_freq_cal(uint32_t v1, uint32_t v2)
{
    if(0 != v1){
        /* calculate the duty cycle value */
        oscilloscope.meter.duty = ((uint64_t)v2 * 1000) / v1;
        /* calculate the frequency value */
        oscilloscope.meter.freq = 120000000 / v1;
        oscilloscope.config.freq_valid = 1;
    }else{
        oscilloscope.config.freq_valid = 0;
    }
    oscilloscope.freq_invalid_cnt = xTaskGetTickCount(); //同步计时时间
}

void osc_get_meter_str(char *str, uint8_t index)
{
    switch(index)
    {
        case kOSCMeter_Max:
            strcpy(str,"最大值:");
            int_to_str(str+strlen(str),oscilloscope.meter.max,3,0,"V");
            break;
        case kOSCMeter_Min:
            strcpy(str,"最小值:");
            int_to_str(str+strlen(str),oscilloscope.meter.min,3,0,"V");
            break;
        case kOSCMeter_Vpp:
            strcpy(str,"峰峰值:");
            int_to_str(str+strlen(str),oscilloscope.meter.vpp,3,0,"V");
            break;
        case kOSCMeter_Freq:
            strcpy(str,"频率:");
            if(oscilloscope.config.freq_valid)
                int_to_str(str+strlen(str),oscilloscope.meter.freq,3,0,"KHz");
            else 
                strcat(str, "--");
            break;
        case kOSCMeter_Period:
            strcpy(str,"周期:");
            if(oscilloscope.config.freq_valid) 
                int_to_str(str+strlen(str),1000000ul/oscilloscope.meter.freq,3,0,"ms");
            else 
                strcat(str, "--");
            break;
        case kOSCMeter_Duty:
            strcpy(str,"占空比:");
            if(oscilloscope.config.freq_valid) 
                int_to_str(str+strlen(str),oscilloscope.meter.duty,1,0,NULL);
            else 
                strcat(str, "--");
            break;
    }
}

void osc_init(void)
{
    oscilloscope.meter.offset = storedata.osc.offset;
    oscilloscope.meter.v_amp = storedata.osc.amp_factor[0];
    oscilloscope.config.reverse = 0;
    oscilloscope.wave1.data_size = 400;
    oscilloscope.config.t_div = 0;
    osc_set_trigger(0, 100); //下降沿，100mV
    osc_select_ch(kBoardOSC_CH_DC, 0);
}

void task_osc(void *para)
{
    osc_init();
    oscilloscope.freq_invalid_cnt = xTaskGetTickCount();
    while(1)
    {
        //if(xSemaphoreTake(xSemaphoreOSC, 200) == pdTRUE) {
            //board_osc_trigger_enable(1);
        //    nvic_irq_enable(EXTI10_15_IRQn, 5, 0);
        //}
        if((xTaskGetTickCount()-oscilloscope.freq_invalid_cnt)>2000) //计时超过2S都没计算频率，认为当前频率值无效
            oscilloscope.config.freq_valid = 0;
        vTaskDelay(pdMS_TO_TICKS(200));
    }           
}
